# -*- coding: utf-8 -*-
# @Time    : 2019/5/26 11:25
# @Author  : Igor
# @File    : ATM.py
# @Software: PyCharm
# @url: https://blog.csdn.net/COCO56/article/details/84403107

import xlrd
import openpyxl


class MainUi(object):

    def get_account(self):
        book = xlrd.open_workbook("accout.xlsx")
        table = book.sheet_by_index(0)
        data_account = table.col_values(0)
        return data_account

    def get_password(self):
        book = xlrd.open_workbook("accout.xlsx")
        table = book.sheet_by_index(0)
        data_password = table.col_values(1)
        return data_password

    def get_money(self, data):
        book = xlrd.open_workbook("accout.xlsx")
        table = book.sheet_by_index(0)
        data_money = table.cell_value(data, 2)
        return data_money

    def add_account(self, username):
        book = openpyxl.load_workbook("accout.xlsx")
        table = book[book.sheetnames[0]]
        table_row_len = table.max_row
        table.cell(table_row_len, 1).value = username
        book.save("accout.xlsx")

    def add_password(self, password):
        book = openpyxl.load_workbook("accout.xlsx")
        table = book[book.sheetnames[0]]
        table_row_len = table.max_row
        table_column_len = table.max_column
        table.cell(table_row_len, table_column_len - 1).value = password
        book.save("accout.xlsx")

    def add_money(self, money):
        book = openpyxl.load_workbook("accout.xlsx")
        table = book[book.sheetnames[0]]
        table_row_len = table.max_row
        table_column_len = table.max_column
        table.cell(table_row_len, table_column_len).value = money
        book.save("accout.xlsx")

    def main_ui(self):
        print("-------------------欢迎来到ATM取款机,请选择您的操作-------------------")
        print("|                  1.登陆      2.注册      3.退出                    |")
        print("-------------------********************************-------------------")
        choose = int(input())
        if choose == 1:
            self.login_action()
        elif choose == 2:
            self.register_action()
        else:
            print("-------------------********************************-------------------")
            print("|                  您已退出ATM系统,欢迎下次再次使用                  |")
            print("-------------------********************************-------------------")

    def login_action(self):
        status = True
        while status:
            print("-------------------********************************-------------------")
            print("|                         请输入您的账号：                           |")
            print("|                         请输入您的密码：                           |")
            print("-------------------********************************-------------------")
            username = input()
            password = input()
            for i in range(len(self.get_account())):
                if username == self.get_account()[i] and password == self.get_password()[i]:
                    login = LoginUi(username)
                    login.main_login_ui()
                    status = False

    def register_action(self):
        print("-------------------********************************-------------------")
        print("|                       输入您的账号密码开始注册！                   |")
        print("|                         请输入您的账号：                           |")
        print("|                         请输入您的密码：                           |")
        print("-------------------********************************-------------------")
        reg_username = input()
        reg_password = input()
        for i in range(len(self.get_account())):
            if reg_username == self.get_account()[i]:
                print("-------------------********************************-------------------")
                print("|                   输入您的账号已被注册！请重试！                   |")
                print("-------------------********************************-------------------")
                self.register_action()
            else:
                self.add_account(reg_username)
                self.add_password(reg_password)
                self.add_money(0)
                print("-------------------********************************-------------------")
                print("|                      您的账号已注册成功！                      |")
                print("-------------------********************************-------------------")
                main = MainUi()
                main.main_ui()


class LoginUi(object):

    def __init__(self, username):
        self.username = username

    def main_login_ui(self):
        print("-------------------******登录成功,请选择操作********-----------------")
        print("|                1.存款    2.取款     3.转账    4.退出              |")
        print("-------------------********************************------------------")
        choose = int(input())
        if choose == 1:
            self.save()
        elif choose == 2:
            self.get()
        elif choose == 3:
            self.transfer()
        else:
            print("-------------------********************************-------------------")
            print("|                  您已退出ATM系统,欢迎下次再次使用                  |")
            print("-------------------********************************-------------------")

    def save(self):
        print("-------------------********************************-------------------")
        print("|                           请输入存款金额:                          |")
        print("-------------------********************************-------------------")
        money = float(input())
        main = MainUi()
        for i in range(len(main.get_account())):
            if self.username == main.get_account()[i]:
                book = openpyxl.load_workbook("accout.xlsx")
                table = book[book.sheetnames[0]]
                table.cell(i + 1, 3).value = money + float(main.get_money(i))
                book.save("accout.xlsx")
                data = float(main.get_money(i))
        print("-------------------********************************---------------------")
        print("|                         存入成功,余额为{}元                     |".format(data))
        print("-------------------********************************---------------------")

    def get(self):
        print("-------------------********************************-------------------")
        print("|                           请输入取款金额:                          |")
        print("-------------------********************************-------------------")
        money = float(input())
        main = MainUi()
        for i in range(len(main.get_account())):
            if self.username == main.get_account()[i]:
                book = openpyxl.load_workbook("accout.xlsx")
                table = book[book.sheetnames[0]]
                if float(main.get_money(i)) >= money:
                    table.cell(i + 1, 3).value = float(main.get_money(i)) - money
                    book.save("accout.xlsx")
                    data = float(main.get_money(i))
                    print("-------------------********************************------------------")
                    print("|                         取款成功,余额为{}元                  |".format(data))
                    print("-------------------********************************------------------")
                else:
                    print("-------------------********************************-------------------")
                    print("|                        您的余额不足,无法取款！                     |")
                    print("-------------------********************************-------------------")

    def transfer(self):
        print("-------------------********************************-------------------")
        print("|                           请输入转账账号:                          |")
        print("|                           请输入转账金额:                          |")
        print("-------------------********************************-------------------")
        account = input()
        money = float(input())
        if account not in main.get_account():
            print("-------------------********************************-------------------")
            print("|                      输入的账号不存在！                            |")
            print("-------------------********************************-------------------")

        if self.get_my_balance(self.username) - money >= 0:
            new_my_balance = self.get_my_balance(self.username) - money
            print(new_my_balance)
            new_other_balance = self.get_othrt_balance(account) + money
            print(new_other_balance)
            book = openpyxl.load_workbook("accout.xlsx")
            table = book[book.sheetnames[0]]
            table.cell(self.get_account_where(self.username) + 1, 3).value = new_my_balance
            table.cell(self.get_account_where(account) + 1, 3).value = new_other_balance
            book.save("accout.xlsx")
            print("-------------------********************************-------------------")
            print("|                            转账成功！                              |")
            print("-------------------********************************-------------------")
        else:
            print("-------------------********************************-------------------")
            print("|                            账号余额不足！                          |")
            print("-------------------********************************-------------------")

    def get_account_where(self, username):
        # 拿到账号在excel的行位置
        for i in range(len(main.get_account())):
            if username == main.get_account()[i]:
                return i

    def get_my_balance(self, username):
        for i in range(len(main.get_account())):
            if username == main.get_account()[i]:
                my_balance = main.get_money(i)  # 拿到自己的余额
                return my_balance

    def get_othrt_balance(self, account):
        for j in range(len(main.get_account())):
            if account == main.get_account()[j]:
                book = openpyxl.load_workbook("accout.xlsx")
                table = book[book.sheetnames[0]]
                other_balance = table.cell(j + 1, 3).value  # 拿到要转账人的余额
                return other_balance


if __name__ == "__main__":
    main = MainUi()
    main.main_ui()

